package com.desmond.squarecamera.slice;

import com.bumptech.glide.Glide;
import com.desmond.squarecamera.ResourceTable;
import com.desmond.squarecamera.util.CameraConstants;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.utils.net.Uri;

public class MainAbilitySlice extends AbilitySlice {

    private static final int REQUEST_CAMERA = 0;
    private static final int REQUEST_CAMERA_PERMISSION = 1;

    private Button mBtnLaunch;
    private Image mResultImage;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        initView();
    }

    private void initView() {

        mBtnLaunch = (Button) findComponentById(ResourceTable.Id_btn_launch_camera);
        mResultImage = (Image) findComponentById(ResourceTable.Id_image);

        mBtnLaunch.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                launch();
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private void launch() {
        Intent intent = new Intent();

        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(getBundleName())
                .withAbilityName(CameraConstants.CAMERA_ABILITY_NAME)
                .build();

        intent.setOperation(operation);
        startAbilityForResult(intent, 0x100);
    }

    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
        if (resultCode == CameraConstants.CAMERA_RESULT_CODE) {
            Uri uri = resultData.getUri();
            if (uri != null) {
                Glide.with(getContext()).load(uri).into(mResultImage);
            }
        }
    }

}
